package deep.projects.newsapp.utils;

import android.content.Context;
import com.squareup.timessquare.CalendarCellDecorator;
import com.squareup.timessquare.CalendarPickerView;
import com.squareup.timessquare.DefaultDayViewAdapter;
import com.wuxinwudai.adlg.ContainerDialog;
import com.wuxinwudai.android.util.DateTimeUtils;
import com.wuxinwudai.android.util.ToastUtils;

import java.util.Calendar;
import java.util.Collections;
import java.util.Date;

/**
 * @author 吾心无待 于2016年03月12日
 */
public class TimeSquareDialogUtils {
    public static ContainerDialog showDialog(final Context context){
        ContainerDialog dialog = ContainerDialog.create(context).setTitle("请选择日期").setContainerHeight(500);
        final CalendarPickerView calendar = new CalendarPickerView(context, null);
        final Calendar nextYear = Calendar.getInstance();
        nextYear.add(Calendar.YEAR, 1);//支持的最大日期
        final Calendar lastYear = Calendar.getInstance();
        lastYear.add(Calendar.YEAR, -1);//支持的最小日期
        calendar.setDecorators(Collections.<CalendarCellDecorator>emptyList());
        calendar.setCustomDayView(new DefaultDayViewAdapter());
        calendar.init(lastYear.getTime(), nextYear.getTime())
                .inMode(CalendarPickerView.SelectionMode.SINGLE)
                .withSelectedDate(new Date());
        dialog.setChildView(calendar)
            .setOnConfirmClickListener(new ContainerDialog.OnConfirmClickListener() {
                @Override
                public void confirm(ContainerDialog containerDialog) {
                    if (calendar.getSelectedDate() == null) {
                        ToastUtils.showShort(context, "请选择日期！");
                    } else {
                        ToastUtils.showShort(context, DateTimeUtils.CHINESE_DATE_FORMAT.format(calendar.getSelectedDate()));
                    }
                }
            });
        dialog.show();
        return dialog;
    }
}
